package com.cloudpan.controller;

import com.cloudpan.common.R;
import com.cloudpan.entity.bo.SysUserSpaceBo;
import com.cloudpan.entity.param.InitTaskParam;
import com.cloudpan.entity.pojo.SysUploadTask;
import com.cloudpan.entity.vo.TaskInfoVo;
import com.cloudpan.service.SysUploadTaskService;
import com.cloudpan.service.SysUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 分片上传-分片任务记录 前端控制器
 * </p>
 *
 * @author Code Generator
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/sys/task")
@RequiredArgsConstructor
@Api(value = "文件上传控制层", tags = "文件上传控制层")
public class SysUploadTaskController {

    private final SysUploadTaskService taskService;

    private final SysUserInfoService userInfoService;


    @PostMapping("/uploadfile")
    @ApiOperation(value = "小文件直接上传，不需要分片(小于5m)")
    public R uploadfile(@RequestParam("file") MultipartFile file,
                        InitTaskParam param){
        String result = taskService.uploadfile(file,param);
        return R.ok(result);
    }

    @GetMapping("/checkUserSpace")
    @ApiOperation(value = "用户在上传文件时,先校验查看用户是否还有存储空间")
    public R userSpace(@RequestBody SysUserSpaceBo userSpaceBo){
        Boolean result = userInfoService.userSpace(userSpaceBo);
        if (result){
            return R.ok(true);
        }
        return R.failed("你的存储空间不足，请联系管理员扩容~");
    }

    @GetMapping("/{identifier}")
    @ApiOperation(value = "获取上传进度")
    public R<TaskInfoVo> taskInfo(@PathVariable("identifier") String identifier) {
        TaskInfoVo taskInfo = taskService.getTaskInfo(identifier);
        return R.ok(taskInfo);
    }

    @PostMapping("/{bucketName}")
    @ApiOperation(value = "创建一个上传任务")
    public R<TaskInfoVo> initTask(@Valid @RequestBody InitTaskParam param) {
        TaskInfoVo task = taskService.initTask(param);
        return R.ok(task);
    }

    @GetMapping("/{identifier}/{partNumber}")
    @ApiOperation(value = "获取每个分片的预签名上传地址")
    public R<Object> preSignUploadUrl(@PathVariable("identifier") String identifier,
                                      @PathVariable("partNumber") Integer partNumber) {
        SysUploadTask task = taskService.getByIdentifier(identifier);
        if (task == null) {
            return R.failed("分片任务不存在");
        }
        Map<String, String> params = new HashMap<>();
        params.put("partNumber", partNumber.toString());
        params.put("uploadId", task.getUploadId());
        String url = taskService.genPreSignUploadUrl(task.getBucketName(), task.getObjectKey(), params);
        return R.ok(url);
    }

    @PostMapping("/merge/{identifier}")
    @ApiOperation(value = "合并分片")
    public R<String> merge(@PathVariable("identifier") String identifier) {
        taskService.merge(identifier);
        return R.ok();
    }


}

